/******************************************************************************中
 * @brief: systask.c
 * @Author:   aple
 * @Date: 2023-01-05 20:47:29
 * @LastEditors: aple
 * @LastEditTime: 2023-01-05 21:03:26
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "udisk_task.h"
#include "fx_api.h"
#include "common.h"

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
void pdf_author_demo(void);

/*****************************************************************************
 * @brief   var.
 *****************************************************************************/
extern FX_MEDIA usb_disk;
extern FX_FILE usb_file;

/*****************************************************************************
 * @brief   system task.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void spdf_task(char en)
{
	dbg_print("FILEX.PDF FILE CREAT!\n");

	/* Create a file called TEST.TXT in the root directory.  */
	udisk_st.fil_opt_st = fx_file_create(&usb_disk, "FILEX.PDF");

	/* Check the create udisk_st.fil_opt_st.  */
	if (udisk_st.fil_opt_st != FX_SUCCESS)
	{
		/* Check for an already created udisk_st.fil_opt_st. This is expected on the
		   second pass of this loop!  */
		if (udisk_st.fil_opt_st != FX_ALREADY_CREATED)
		{
			/* Create error, break the loop.  */
			dbg_print("FILEX.PDF creat: ERROR\n");
			return;
		}
		else
		{
			dbg_print("FILEX.PDF creat: FX_ALREADY_CREATED\n");
		}
	}
	else
	{
		dbg_print("FILEX.PDF creat: OK\n");
	}

	/* file open and write */
	udisk_st.fil_opt_st = fx_file_open(&usb_disk, &usb_file, "FILEX.PDF", FX_OPEN_FOR_WRITE);
	if (udisk_st.fil_opt_st == FX_SUCCESS)
	{
		pdf_author_demo();

		dbg_print("FILEX.PDF FILE CREAT END!\n");
	}
	else
	{
		dbg_print("FILEX.PDF open: FAIL!\n");
	}

	udisk_st.fil_opt_st = fx_file_close(&usb_file);
}

int spdf_disk_out(uint8_t *buf, int len)
{
	uint32_t len_back = 0;

	udisk_st.fil_opt_st = fx_file_write(&usb_file, buf, len);

	if (udisk_st.fil_opt_st != FX_SUCCESS)
	{
		dbg_print("FILEX.PDF write: FAIL!\n");
	}

	return len;
}